<template>
  <div class="dashboard-container">
    <div class="dashboard-text" style="min-height: 695px;">
      <el-row style="margin-bottom: 15px;line-height: 30px;">
        <el-col :span="12">
          <span style="font-size: 18px;font-weight: bold;">角色管理</span>
        </el-col>
        <el-col :span="12" style="text-align: right;">
          <el-button type="primary" style="width: 13%;" @click="dialogFormVisible = true">创建角色</el-button>
        </el-col>
      </el-row>
      <!-- 需要封装开始 -->
      <el-dialog title="新增角色" :visible.sync="dialogFormVisible" class="tjcd">
        <el-form :model="form" :rules="rules">
          <el-form-item label="角色名称：" prop="Rolename" :label-width="formLabelWidth">
            <el-input v-model="form.Rolename" autocomplete="off" />
          </el-form-item>
          <el-form-item size="mini" label="分配菜单：" :label-width="formLabelWidth">
            <el-tree
              :data="data"
              show-checkbox
              node-key="id"
              :props="defaultProps"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 需要封装结束 -->
      <el-row>
        <el-col :span="24">
          <el-table
            :data="tableData"
            border
            style="width: 100%"
          >
            <el-table-column
              prop="roleId"
              align="center"
              label="编号"
            />
            <el-table-column
              prop="roleName"
              align="center"
              label="角色名称"
            />
            <el-table-column
              prop="name"
              align="center"
              label="是否启用"
            >
              <el-switch v-model="value1" />
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
            >
              <template slot-scope="scope">
                <el-button type="text" size="small">编辑</el-button>
                <el-button type="text" size="small" @click="handleClick(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!--      <el-row style="margin: 25px 0 15px;line-height: 26px;text-align: right;">
        <el-col :span="24">
          <div class="block">
            <el-pagination
              :current-page="currentPage4"
              :page-sizes="[100, 200, 300, 400]"
              :page-size="100"
              layout="total, sizes, prev, pager, next, jumper"
              :total="400"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-col>
      </el-row> -->
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getRoleList, addRole, deleteRole, updateRole } from '../../api/user.js'
export default {
  name: 'Dashboard',
  data() {
    return {
      data: [{
        id: 1,
        label: '一级 1',
        children: [{
          id: 4,
          label: '二级 1-1',
          children: [{
            id: 9,
            label: '三级 1-1-1'
          }, {
            id: 10,
            label: '三级 1-1-2'
          }]
        }]
      }, {
        id: 2,
        label: '一级 2',
        children: [{
          id: 5,
          label: '二级 2-1'
        }, {
          id: 6,
          label: '二级 2-2'
        }]
      }, {
        id: 3,
        label: '一级 3',
        children: [{
          id: 7,
          label: '二级 3-1'
        }, {
          id: 8,
          label: '二级 3-2'
        }]
      }],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      dialogFormVisible: false,
      form: {
        Rolename: ''
      },
      rules: {
        Rolename: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      formLabelWidth: '100px',
      value1: true,
      currentPage4: 1,
      tableData: [],
      input: '',
      options: [{
        value: '选项1',
        label: '黄金糕'
      }, {
        value: '选项2',
        label: '双皮奶'
      }, {
        value: '选项3',
        label: '蚵仔煎'
      }, {
        value: '选项4',
        label: '龙须面'
      }, {
        value: '选项5',
        label: '北京烤鸭'
      }],
      value: ''
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleClick(row) {
      deleteRole(row.roleIds).then(response => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch(() => {
        this.$message({
          message: '删除失败',
          type: 'success'
        })
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    // 获取角色列表
    getList() {
      getRoleList().then(response => {
        this.tableData = response.page.list
      }).catch(() => {
      })
    },
    // 修改角色
    updateRole(row) {
      var param = {}
      updateRole(param).then(response => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
      }).catch(() => {
        this.$message({
          message: '修改失败',
          type: 'success'
        })
      })
    },
    // 添加角色
    addRole() {
      addRole(this.loginForm).then(response => {
      }).catch(() => {
      })
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  }
}
</script>
<style>
  .tjcd /deep/ .el-dialog__header{
    line-height: 0 !important;
  }
  .tjcd /deep/ .el-dialog__body{
    padding-top: 15px !important;
  }
  .tjcd /deep/ .el-dialog{
    width: 30% !important;
  }
</style>
<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 15px;
  }
  &-text {
    padding: 15px;
    font-size: 30px;
    line-height: 46px;
    background-color: #fff;
  }
}
</style>
